<?php
namespace Api\Provider\Admin;
use Api\Provider\IndexProvider;
use Think\Log;

/**
 * Description of ConstantProvider
 */

set_time_limit(0);
class DataCleanProvider extends IndexProvider
{
    
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 公共配置参数获取
     * @param $param
     */
    public function clean_sheet($param)
    {
        //echo APP_PATH; exit;
        Log::writeInfo('start to clean sheet, param:' .json_encode($param),'data_clean');
        
        while(true) {
            $sql = 'select name, count(name) from sheet group by name having count(name)>1 limit 50';
            $list = M('')->query($sql);
            if(empty($list)) break;
            
            $repeatedSheetNames = array_unique(array_column($list, 'name'));
            $map['name'] = array('in', $repeatedSheetNames);
            $sheetList = M('Sheet')->where($map)->select();
            
            $sheetMap= array();
            foreach($sheetList as $key=> $val) {
                $sheetMap[$val['name']][] = $val['id'];
            }
            
            $removeSheetIds = array();
            foreach ($sheetMap as $key=>$val) {
                $removeRepeatedSheet = array_slice($val, 1);
                
                $removeSheetIds = array_merge($removeSheetIds, $removeRepeatedSheet);
            }
            
            unset($map);
            $map['id'] = array('in', $removeSheetIds);
            $sheetModel = M('Sheet');
            $sheetModel->where($map)->delete();
            echo $sheetModel->getLastSql().';';
            
            unset($map);
            $map['sheet_id'] = array('in', $removeSheetIds);
            $imageSheetModel = M('ImageSheet');
            $imageSheetModel->where($map)->delete();
            echo $imageSheetModel->getLastSql().';';
        }
        echo 'finish';exit;
    }
}
